﻿using System.Linq;
using System.Web;
using System.Web.SessionState;
using Bricks.Caching;

namespace Motar.Web.Caching
{

    public class HttpSessionCacheFacade : CacheFacadeBase<HttpSessionState>, IHttpSessionCacheFacade
    {

        /// <summary>
        /// 
        /// </summary>
        public HttpSessionCacheFacade() :
            base(() => HttpContext.Current.Session)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public override void Add(string key, object value)
        {
            Execute(session => session.Add(key, value));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public override bool Contains(string key)
        {
            return Execute<bool>(session => session.Keys.Cast<object>().Contains<object>(key));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public override object Get(string key)
        {
            return Execute<object>(session => session[key]);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        public override void Remove(string key)
        {
            Execute(session => session.Remove(key));
        }

        /// <summary>
        /// 
        /// </summary>
        public override void Flush()
        {
            Execute(session => session.Clear());
        }
    }
}
